package org.richin.file.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.Map.Entry;

import org.richin.system.util.Assert;

public class PropertiesUtils {

	public static Properties load(File file) throws IOException {
		InputStream in = new FileInputStream(file);
		try {
			return load(in);
		} finally {
			if (in != null)
				in.close();
		}
	}

	/**
	 * 删除属性
	 *
	 * @param file
	 *            properties文件所在路径
	 * @param key
	 *            属性名称
	 */
	public static void deleteProperties(File file, String key) {
		FileOutputStream fo = null;

		try {
			Properties pt = load(file);
			fo = new FileOutputStream(file);
			Iterator it = pt.keySet().iterator();

			while (it.hasNext()) {
				if (it.next().equals(key)) {
					it.remove();
					pt.store(fo, null);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fo.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 将属性与值写入properties资源属性文件中
	 *
	 * @param file
	 *            属性文件的路径
	 * @param map
	 *            存放多个属性-值对
	 * @param comments
	 *            注释内容
	 */
	public static void saveProperties(File file, Map<String, String> map, String comments) {

		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(file);

			Properties pt = new Properties();
			Set<Entry<String, String>> set = map.entrySet();
			Iterator<Entry<String, String>> iterator = set.iterator();
			while (iterator.hasNext()) {
				Entry<String, String> entry = iterator.next();
				pt.setProperty(entry.getKey(), entry.getValue());
			}

			pt.store(fos, comments);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

	public static Properties load(InputStream in) throws IOException {
		Properties prop = new Properties();
		prop.load(in);
		return prop;
	}

	public static Properties loadXml(File file) throws IOException {
		InputStream in = new FileInputStream(file);
		try {
			return loadXml(in);
		} finally {
			if (in != null)
				in.close();
		}
	}

	public static Properties loadXml(InputStream in) throws IOException {
		Properties prop = new Properties();
		prop.loadFromXML(in);
		return prop;
	}

	public static boolean isXmlProperties(String name) {
		return name.endsWith(".xml.properties");
	}

	public static boolean isProperties(String name) {
		return name.endsWith(".properties");
	}

	/**
	 * Load a properties file.
	 * The path must include the package name(s),
	 * if any, in system directory format and
	 * include the properties extension.
	 *
	 * @param path The path to the properties file
	 * @return The initialized Properties object
	 */

	public static final Properties loadProperties(String propfile) {

		InputStream i = null;
		Properties p = null;

		i = getResourceAsStream(propfile);

		if (i != null) {

			try {
				p = new Properties();
				p.load(i);
				i.close();
			}

			catch (java.io.IOException e) {
				System.out.println(e);
			}

			finally {
				i = null;
			}
		}

		return p;

	}

	/**
	 This method will search for <code>resource</code> in different
	 places. The rearch order is as follows:
	 <ol>
	 <p><li>Search for <code>resource</code> using the thread context
	 class loader under Java2. If that fails, search for
	 <code>resource</code> using the class loader that loaded this
	 class (<code>Loader</code>). Under JDK 1.1, only the the class
	 loader that loaded this class (<code>Loader</code>) is used.
	 <p><li>Try one last time with
	 <code>ClassLoader.getSystemResource(resource)</code>, that is is
	 using the system class loader in JDK 1.2 and virtual machine's
	 built-in class loader in JDK 1.1.
	 </ol>
	 */
	private static java.io.InputStream getResourceAsStream(String propfile) {

		ClassLoader classLoader = null;
		java.io.InputStream is = null;

		try {

			classLoader = Thread.currentThread().getContextClassLoader();
			if (classLoader != null) {
				is = classLoader.getResourceAsStream(propfile);
				if (is != null) {
					return is;
				}
			}

			// We could not find resource. Ler us now try with the
			// classloader that loaded this class.
			classLoader = PropertiesUtils.class.getClassLoader();
			is = classLoader.getResourceAsStream(propfile);
			if (is != null) {
				return is;
			}
		} catch (Exception e) {
		}

		try {

			is = new java.io.FileInputStream(propfile);
		} catch (Exception e) {
		}

		return is;
	}

	public static String getResourceRealPath(String path) {

		ClassLoader classLoader = null;
		String s = null;

		try {

			classLoader = Thread.currentThread().getContextClassLoader();
			if (classLoader != null) {
				s = classLoader.getResource("/").toString();
				if (s != null) {
					return s;
				}
			}

			// We could not find resource. Ler us now try with the
			// classloader that loaded this class.
			classLoader = PropertiesUtils.class.getClassLoader();
			s = classLoader.getResource("/").toString();
			if (s != null) {
				return s;
			}
		} catch (Exception e) {
		}

		return s;
	}
	public static void mergePropertiesIntoMap(Properties props, Map<String,String> map) {
		Assert.notNull(map,"Map must not be null");
		if (props != null) {
			for(Object obj:props.keySet()){
				String key=(String)obj;
				map.put(key, props.getProperty(key));
			}
		}
	}
}
